package com.alinesno.cloud.gateway.admin.proxy.socket;

import org.apache.commons.lang3.StringUtils;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alinesno.cloud.gateway.admin.entity.GateBusinessServerEntity;
import com.alinesno.cloud.gateway.admin.proxy.BaseProxy;
import com.alinesno.cloud.gateway.core.dispather.BaseDispather;
import com.alinesno.cloud.gateway.core.dispather.mina.MinaSocketProxyFactory;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.BusinessServerBean;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.MessageBean;
import com.alinesno.cloud.gateway.core.dispather.mina.bean.MessageBuilder;
import com.alinesno.cloud.gateway.core.dispather.mina.client.MinaClient;

/**
 * Socket代理转发
 * 
 * @author WeiXiaoJin
 * @since 2020年1月31日 下午19:45:08
 */
@Service(BaseDispather.SOCKET_PROXY_PREFIX + BaseDispather.SOCKET_PROXY_SOCKET)
public class MinaSocketProxy extends BaseProxy implements MinaSocketProxyFactory {
	
	private static final Logger log = LoggerFactory.getLogger(MinaSocketToHttpProxy.class) ;

	@Override
	protected MessageBean proxy(BusinessServerBean server, IoSession session, MessageBean message, GateBusinessServerEntity e, String clientIP) {
		log.debug("反向代理请求proxy");
	
		if(StringUtils.isBlank(server.getForwardIp()) || server.getForwardPort() == 0) {
			return MessageBuilder.proxy("代理服务器异常.") ; 
		}
		
		message = MinaClient.send(message, server) ; 
		log.debug("proxy message : {}" , message);
		
		return message;
	} 

}
